package com.example.demo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.pojo.User;

@Controller
public class FirstThymeleaf {
	// thymeleaf的日期操作和字符串操作
	@RequestMapping("/show")
	public String show(Model model) {
		model.addAttribute("msg", "this is first thymeleaf");
		model.addAttribute("now", new Date());
		return "index";
	}

	// thymeleaf的条件操作
	@RequestMapping("/show2")
	public String show2(Model model) {
		model.addAttribute("sex", "男");
		model.addAttribute("id", 3);
		return "index2";
	}

	// thymeleaf循环迭代操作，操作的是list
	@RequestMapping("/show3")
	public String show3(Model model) {
		List<User> userList = new ArrayList<User>();
		User user1 = new User(1, "张加林", "男");
		User user2 = new User(2, "陈邱跃", "女");
		User user3 = new User(3, "张清莲", "女");
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		model.addAttribute("userList", userList);
		return "index3";
	}

	// thymeleaf循环迭代操作，操作的是list
	@RequestMapping("/show4")
	public String show4(Model model) {
		Map<String, User> userMap = new HashMap<String, User>();
		User user1 = new User(1, "张加林", "男");
		User user2 = new User(2, "陈邱跃", "女");
		User user3 = new User(3, "张清莲", "女");
		userMap.put("user1", user1);
		userMap.put("user2", user2);
		userMap.put("user3", user3);
		model.addAttribute("userMap", userMap);
		return "index4";
	}

	// thymeleaf域对象
	@RequestMapping("/show5")
	public String show5(HttpServletRequest request, Model model) {
		request.setAttribute("req", "request");
		request.getSession().setAttribute("session", "session");
		request.getSession().getServletContext().setAttribute("context", "context");
		return "index5";
	}

	// thymeleaf url的传递方式
	@RequestMapping("/show6")
	public String show6(String page) {
		System.out.println(page);
		return "index6";
	}

	// thymeleaf url的传递方式
	@RequestMapping("/show7/{page}")
	public String show61(@PathVariable String page) {
		System.out.println(page);
		return "index3";
	}
}
